package com.diver.flinkdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.diver.flinkdemo.entity.xxt.Fence;
import com.diver.flinkdemo.mapper.mysqlMapper.FenceMapper;
import com.diver.flinkdemo.mapper.mysqlMapper.PersonTerminalInfoMapper;
import com.diver.flinkdemo.service.IFenceService;
import com.diver.flinkdemo.utils.DynamicDataSourceUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @author zhangyj
 * @date 2023/3/6
 */
@Slf4j
public class FenceServiceImpl extends ServiceImpl<FenceMapper, Fence> implements IFenceService {

    private FenceMapper mapper;
    private SqlSession session = null;

    @Override
    public Fence getSchoolCircleFenceInfo(String departId) {
        try {
            session = DynamicDataSourceUtil.mysqlSessionFactory.openSession();
            mapper = session.getMapper(FenceMapper.class);
            return mapper.getSchoolCircleFenceInfo(departId);
        } finally {
            endOfSession(session);
        }
    }

    @Override
    public void updateWifiListById(String wifiList, String areaId) {
        try {
            session = DynamicDataSourceUtil.mysqlSessionFactory.openSession();
            mapper = session.getMapper(FenceMapper.class);
            mapper.updateWifi(wifiList ,areaId);
        } finally {
            endOfSession(session);
        }
    }

    @Override
    public List<Fence> getFenceByPersonId(String personId) {
        try {
            session = DynamicDataSourceUtil.mysqlSessionFactory.openSession();
            mapper = session.getMapper(FenceMapper.class);
            LambdaQueryWrapper<Fence> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Fence::getPersonId,personId);
            wrapper.eq(Fence::getDelFlag,0);
            wrapper.eq(Fence::getStatus,1);
            List<Fence> fenceList = mapper.selectList(wrapper);
            return fenceList;
        } finally {
            endOfSession(session);
        }
    }
    public void endOfSession(SqlSession session) {
        //关闭sqlSession
        if (session != null) {
            log.info("session commit -> session close");
//            session.commit();
            session.close();
        }
    }

}
